package com.myjoje.base;

import com.myjoje.bean.UserBean;
import com.myjoje.config.shiro.ShiroUtil;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * BaseService，继承后可以方便很多操作
 */
public class BaseService extends BaseUtil {

    /**
     * 默认密码
     */
    public String getDefaultPassword() {

        return ShiroUtil.getMd5("123");
    }

    /**
     * 获取当前用户
     */
    public UserBean getCurrentUser() {

        return ShiroUtil.getCurrentUser();
    }

    /**
     * 获取当前用户id
     */
    public String getCurrentUserId() {
        UserBean user = getCurrentUser();
        return user == null ? null : user.getId();
    }

    /**
     * 根据传入的类型获取spring管理的Bean
     *
     * @param clazz   类型
     * @param request 请求对象
     */
    public <T> T getSpringManagedBean(Class<T> clazz, HttpServletRequest request) {
        BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
        return factory.getBean(clazz);
    }

    /**
     * 获取请求对象
     */
    public HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

}
